package com.yellowpage.bigdata.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.bigdata.data.NspFatallogImeiDayregionSum;
import com.yellowpage.bigdata.service.NspFatallogImeiDayregionSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;

public class NspFatallogImeiDayregionSumAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;
	private RegionService regionService;
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<NspFatallogImeiDayregionSum> nspFatallogImeiDayregionSumList = new ArrayList<NspFatallogImeiDayregionSum>();
	private List<Map> resultList = new ArrayList<Map>();

	private NspFatallogImeiDayregionSum nspFatallogImeiDayregionSum;
	private NspFatallogImeiDayregionSumService nspFatallogImeiDayregionSumService;
	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_fatallogImeiDayregionSumId;
	private String query_requestType;
	private String query_imeiDayCount;
	private String query_imeiAllCount;
	private String query_imeiNewCount;
	private String query_regionCode;
	private String query_createUser;
	private String query_logDate;
	private String query_createTime;
	private String query_note;
	private String query_logDate_end;

	private Map requestTypeMap = new HashMap();
	private Map query_regionCodeMap =new HashMap();

	public Map getQuery_regionCodeMap() {
		return query_regionCodeMap;
	}

	public void setQuery_regionCodeMap(Map query_regionCodeMap) {
		this.query_regionCodeMap = query_regionCodeMap;
	}
	
	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
    public String getRegionSql(){
    	  Map session = ActionContext.getContext().getSession();
    	  User user = (User) session.get("userObject");
    	  String function_code= "OMC_ABILITYCALL_LOG_COUNT_DAY_REGIONDATA";
    	  String regionsql= regionService.getRegionclauseByUserFunction(user.getUserId(), function_code, "REGION_CODE");
    	 return regionsql;
    }

	public void prepare() {
		super.prepare();
		// 加载请求类型
		query_regionCodeMap = nspFatallogImeiDayregionSumService.getRegionMap(getRegionSql());
		requestTypeMap = nspFatallogImeiDayregionSumService.getrequestTypeMap();
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspFatallogImeiDayregionSum = nspFatallogImeiDayregionSumService
					.getNspFatallogImeiDayregionSum(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();
		Map queryMap = new HashMap();
//		Map queryMap_1 = new HashMap();
		queryMap.put("requestType", query_requestType);
//		queryMap_1.put("regionCode", query_regionCode + "%");
//		if(query_regionCode.trim().equals("")){
			queryMap.put("regionCode", query_regionCode);
//		}else{
//			queryMap.put("regionCode", nspFatallogImeiDayregionSumService.queryregion(queryMap_1).get(0).get("REGION_CODE"));
//		}
		queryMap.put("logDate", query_logDate);
		queryMap.put("query_logDate_end", query_logDate_end);
		
		resultList = nspFatallogImeiDayregionSumService
				.queryNspFatallogImeiDayregionSumByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		nspFatallogImeiDayregionSumService
				.saveNspFatallogImeiDayregionSum(nspFatallogImeiDayregionSum);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (nspFatallogImeiDayregionSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
		if (nspFatallogImeiDayregionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogImeiDayregionSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspFatallogImeiDayregionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}

		this.setValidatorResult("/bigdate/NspFatallogImeiDayregionSumAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		nspFatallogImeiDayregionSum = nspFatallogImeiDayregionSumService
				.getNspFatallogImeiDayregionSum(id);

		return RESULT_EDIT;
	}

	public String update() {

		nspFatallogImeiDayregionSumService
				.updateNspFatallogImeiDayregionSum(nspFatallogImeiDayregionSum);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (nspFatallogImeiDayregionSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
		if (nspFatallogImeiDayregionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogImeiDayregionSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspFatallogImeiDayregionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		this.setValidatorResult("/bigdate/NspFatallogImeiDayregionSumUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.nspFatallogImeiDayregionSumService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNspFatallogImeiDayregionSum(
			NspFatallogImeiDayregionSum nspFatallogImeiDayregionSum) {
		this.nspFatallogImeiDayregionSum = nspFatallogImeiDayregionSum;
	}

	public NspFatallogImeiDayregionSum getNspFatallogImeiDayregionSum() {
		return nspFatallogImeiDayregionSum;
	}

	public void setNspFatallogImeiDayregionSumService(
			NspFatallogImeiDayregionSumService nspFatallogImeiDayregionSumService) {
		this.nspFatallogImeiDayregionSumService = nspFatallogImeiDayregionSumService;
	}

	public List<NspFatallogImeiDayregionSum> getNspFatallogImeiDayregionSumList() {
		return nspFatallogImeiDayregionSumList;
	}

	public void setNspFatallogImeiDayregionSumList(
			List<NspFatallogImeiDayregionSum> nspFatallogImeiDayregionSumList) {
		this.nspFatallogImeiDayregionSumList = nspFatallogImeiDayregionSumList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_fatallogImeiDayregionSumId() {
		return this.query_fatallogImeiDayregionSumId;
	}

	public void setQuery_fatallogImeiDayregionSumId(
			String fatallogImeiDayregionSumId) {
		this.query_fatallogImeiDayregionSumId = fatallogImeiDayregionSumId;
	}

	public String getQuery_requestType() {
		return this.query_requestType;
	}

	public void setQuery_requestType(String requestType) {
		this.query_requestType = requestType;
	}

	public String getQuery_imeiDayCount() {
		return this.query_imeiDayCount;
	}

	public void setQuery_imeiDayCount(String imeiDayCount) {
		this.query_imeiDayCount = imeiDayCount;
	}

	public String getQuery_imeiAllCount() {
		return this.query_imeiAllCount;
	}

	public void setQuery_imeiAllCount(String imeiAllCount) {
		this.query_imeiAllCount = imeiAllCount;
	}

	public String getQuery_imeiNewCount() {
		return this.query_imeiNewCount;
	}

	public void setQuery_imeiNewCount(String imeiNewCount) {
		this.query_imeiNewCount = imeiNewCount;
	}

	public String getQuery_regionCode() {
		return this.query_regionCode;
	}

	public void setQuery_regionCode(String regionCode) {
		this.query_regionCode = regionCode;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_logDate() {
		return this.query_logDate;
	}

	public void setQuery_logDate(String logDate) {
		this.query_logDate = logDate;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public String getQuery_logDate_end() {
		return query_logDate_end;
	}

	public void setQuery_logDate_end(String query_logDate_end) {
		this.query_logDate_end = query_logDate_end;
	}

}
